package com.dianrui.agent.fragment;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dianrui.agent.R;
import com.dianrui.agent.activity.LoginActivity;
import com.dianrui.agent.adapter.StationRankAdapters;
import com.dianrui.agent.bean.Rank;
import com.dianrui.agent.event.RankFragmentEvent;
import com.dianrui.agent.okhttp.OkHttpRequest;
import com.dianrui.agent.util.SharedUtil;
import com.dianrui.agent.util.StringUtils;
import com.dianrui.agent.util.ToastUtil;
import com.dianrui.agent.util.Url;
import com.google.gson.JsonObject;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class StationListFragment extends BaseFragment {
    private int mPageIndex = 1;
    private int type=1;
    private StationRankAdapters stationRankAdapters;
    private List<Rank> rankList = new ArrayList<>();

    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;


    @Override
    public int getContentViewId() {
        return R.layout.fragment_station;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        stationRankAdapters = new StationRankAdapters(R.layout.item_stationrank, rankList);
        recyclerView.setAdapter(stationRankAdapters);
        getList(1, false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getList(type, true);
                refreshLayout.finishRefresh();
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPageIndex++;
                getList(type, false);
                refreshLayout.finishLoadMore();
            }
        });


        stationRankAdapters.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Rank rank = rankList.get(position);
                Log.e("点击后",rank.allow_number);
            }
        });
    }

    @Subscribe
    public synchronized void onPageSelected(RankFragmentEvent event) {
        if (event != null) {
            getList(event.getType(), true);
            type = event.getType();
        }
    }

    private void getList(int type, final boolean isRfresh) {
        if (isRfresh) {
            mPageIndex = 1;
        }
        JsonObject rankJson = new JsonObject();
        rankJson.addProperty("p", mPageIndex);
        rankJson.addProperty("type", type);
        OkHttpRequest.getInstance().postTokenRequests(SharedUtil.getUrl() + Url.RANKING, rankJson.toString(), new OkHttpRequest.XCallBack() {

            @Override
            public void onResponse(String result) {

                if (isRfresh) {
                    rankList.clear();
                }
                if (!StringUtils.isEmpty(result) && isAdded()) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if ("1000".equals(jsonObject.optString("status"))) {
                            JSONArray data = jsonObject.optJSONArray("data");
                            if (data.length() > 0) {
                                for (int i = 0; i < data.length(); i++) {
                                    Rank rank = new Rank();
                                    rank.name = data.optJSONObject(i).optString("name");
                                    rank.allow_number = data.optJSONObject(i).optString("allow_number");
                                    rank.city_path = data.optJSONObject(i).optString("city_path");
                                    rank.vehicle_number = data.optJSONObject(i).optString("vehicle_number");
                                    rank.borrow_day = data.optJSONObject(i).optString("borrow_day");
                                    rank.repay_day = data.optJSONObject(i).optString("repay_day");
                                    rankList.add(rank);
                                }
                                stationRankAdapters.setNewData(rankList);
                                refreshLayout.finishRefresh();

                            }
                        }else if ("1001".equals(jsonObject.optString("status"))) {
                            refreshLayout.finishRefresh();
                            ToastUtil.showToast(jsonObject.optString("message"));
                        }else if ("1002".equals(jsonObject.optString("status"))) {
                            refreshLayout.finishRefresh();
                            spUtils.clear();
                            ToastUtil.showToast(jsonObject.optString("message"));
                            JumpActivitys(LoginActivity.class);
                            getActivity().finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }

            @Override
            public void FailedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        if (rankList != null) {
            rankList.clear();
        }

    }
}
